/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#ifndef NET_INSTAWEB_REWRITER_PUBLIC_CACHE_EXTENDER_H_
#define NET_INSTAWEB_REWRITER_PUBLIC_CACHE_EXTENDER_H_

#include "net/instaweb/rewriter/public/resource.h"  // for ResourcePtr
#include "net/instaweb/rewriter/public/resource_slot.h"
#include "net/instaweb/rewriter/public/rewrite_filter.h"
#include "net/instaweb/rewriter/public/rewrite_options.h"
#include "net/instaweb/rewriter/public/rewrite_result.h"
#include "net/instaweb/rewriter/public/server_context.h"
#include "pagespeed/kernel/base/basictypes.h"
#include "pagespeed/kernel/base/string_util.h"

namespace net_instaweb {

class CachedResult;
class HtmlElement;
class ResponseHeaders;
class RewriteContext;
class RewriteDriver;
class Statistics;
class Variable;

// Rewrites resources without changing their content -- just their
// URLs and headers.  The original intent of this filter was limited
// to cache extension.  However, its scope has been expanded to include
// domain sharding and moving static resources to cookieless domains or
// CDNs.
//
// TODO(jmarantz): rename this class to something more generic, like
// RenameUrlFilter or ProxyUrlFilter.
class CacheExtender : public RewriteFilter {
 public:
  static const char kCacheExtensions[];
  static const char kNotCacheable[];

  explicit CacheExtender(RewriteDriver* driver);
  ~CacheExtender() override;

  static void InitStats(Statistics* statistics);

  void StartDocumentImpl() override {}
  void StartElementImpl(HtmlElement* element) override;
  void EndElementImpl(HtmlElement* element) override {}

  const char* Name() const override { return "CacheExtender"; }
  const char* id() const override { return RewriteOptions::kCacheExtenderId; }

  // Creates a nested rewrite for given parent and slot, and returns it.
  // The result is not registered with the parent.
  RewriteContext* MakeNestedContext(RewriteContext* parent,
                                    const ResourceSlotPtr& slot);

 protected:
  bool ComputeOnTheFly() const override;
  RewriteContext* MakeRewriteContext() override;

 private:
  class Context;
  friend class Context;

  RewriteResult RewriteLoadedResource(const ResourcePtr& input_resource,
                                      const OutputResourcePtr& output_resource,
                                      CachedResult* result);

  bool ShouldRewriteResource(const ResponseHeaders* headers, int64 now_ms,
                             const ResourcePtr& input_resource,
                             const StringPiece& url,
                             CachedResult* result) const;

  Variable* extension_count_;
  Variable* not_cacheable_count_;

  DISALLOW_COPY_AND_ASSIGN(CacheExtender);
};

}  // namespace net_instaweb

#endif  // NET_INSTAWEB_REWRITER_PUBLIC_CACHE_EXTENDER_H_
